// 机构管理
<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never" style="border:0px;">
      <div slot="header" style="font-size:14px;font-weight:bold;">
        <span>机构管理</span>
      </div>
    <div class="filter-container">
      <div class="left">
        <el-form :inline="true" class="demo-form-inline" size="medium" label-position="right">
          <el-form-item label="机构名称：" prop="orgName">
            <el-input v-model="listQuery.orgName" clearable class="filter-item" placeholder="请输入机构名称" />
          </el-form-item>
        </el-form>
      </div>
      <div class="right">
        <el-button type="primary"  @click="listByPage()">查询</el-button>
        <el-button @click="restQueryForm()">重置</el-button>
      </div>
    </div>
    <vxe-toolbar ref="xToolbar">
         <template #buttons>
         <el-button type="text" icon="el-icon-plus" @click="addDeptWindow()">新增机构</el-button>
        </template>
    </vxe-toolbar>
    <vxe-table
      v-loading="loading"
      :data="queryData"
      border="none"
      highlight-hover-row
      show-header-overflow
      show-overflow
      align="center"
      :header-cell-style="{ background: '#F7F7F8', color: '#606266' }"
      @cell-dblclick="cellDblclick"
    >
      <vxe-table-column type="index" width="50" title="序号"></vxe-table-column>
      <vxe-table-column title="机构名称" field="orgName"></vxe-table-column>
      <vxe-table-column title="组织架构" width="360">
        <template v-slot="{ row }">
          <el-button type="success" size="mini" small @click="getDeptTree(row)">组织架构</el-button>
        </template>
      </vxe-table-column>
      <vxe-table-column title="创建时间" field="createTime" ></vxe-table-column>
      <vxe-table-column title="状态" field="orgStatus">
        <template slot-scope="scope">
           <span>
              <span>
                <i
                  class="smallCircle"
                  :class="[
                    scope.row.orgStatus === 1 ? 'text_active' : 'text_noActive'
                  ]"
                />{{ scope.row.orgStatus === 1 ? '启用' : '禁用' }}
              </span>
            </span>
        </template>
      </vxe-table-column>
      <vxe-table-column title="操作" fixed="right" width="360">
        <template v-slot="{ row }">
          <el-button type="text" @click="updateDeptWindow(row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button type="text" @click="enableDept(row)" v-if="row.orgStatus == 0">启用</el-button>
          <el-button type="text" @click="disableDept(row)" v-if="row.orgStatus == 1">禁用</el-button>
          <el-button type="text" @click="goOrgProcess(row)">流程管理</el-button>
        </template>
      </vxe-table-column>
    </vxe-table>

    <!--  新增部门 -->
    <vxe-modal
      @close="closeModal"
      v-model="modalUpdate.show"
      width="700px"
      height="350px"
      :title="'新增&修改'"
      class="addModal"
    >
      <el-form :model="modalUpdateForm" title-align="left" title-width="100" ref="modalUpdateForm"  label-width="100px" style="width:80%;margin-top:10px;">
        <el-form-item label="机构名称" prop="orgName" :rules="[{ required: true, message: '请输入机构名称' }]">
          <el-input v-model="modalUpdateForm.orgName" placeholder="请输入机构名称" clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="addOrUpdate('modalUpdateForm')" status="primary">提交</el-button>
          <el-button @click="fresh('modalUpdateForm')" status="danger">取消</el-button>
        </el-form-item>
      </el-form>
    </vxe-modal>
    </el-card>
    <vxe-pager
      class="pager"
      @page-change="changePage"
      :current-page.sync="listQuery.pageNo"
      :page-size.sync="listQuery.pageSize"
      :total="total"
      :page-sizes="[1, 10, 30, 50, 100]"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
    >
    </vxe-pager>
  </div>
</template>

<script>
import { listOrg, deleteOrg, createOrUpdateOrg, } from '@/api/org';
export default {
  name: 'orgIndex',
  data: function () {
    return {
      active: 0,
      queryData: [],
      total: 0,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        orgName: undefined,
      },
      loading: true,
      deptTree: [],
      deptManager: [],
      deptUser: [],
      modalUpdateForm: {
        id: undefined,
        orgName: undefined,
        orgStatus: 1,
      },
      modalUpdate: {
        show: false,
        edit: false,
        originRow: {},
      },
    };
  },
  mounted: function () {
    this.listByPage();
  },


  methods: {
    updateById(data) {
      createOrUpdateOrg(data).then((data) => {
        this.$notify.success({title: '成功',message: '操作成功',});
        this.fresh();
        this.restForm();
      });
    },
    getDeptTree(row) {
      this.$router.push({
        path: '/permision/dept',
        query: { orgId: row.id },
      });
    },
    changePage() {
      this.listByPage();
    },

    deleteById(deptId) {
      this.$confirm('删除后不可恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          deleteOrg(deptId).then((data) => {
            this.fresh();
            this.$notify.success({title: '成功',message: '删除成功',});
          });
        })
        .catch(() => {this.$message({type: 'info',message: '已取消删除',});});
    },
    closeModal() {
      this.modalUpdate.row = {};
      this.modalUpdate.originRow = {};
      this.modalUpdate.show = false;
      this.modalUpdate.edit = false;
    },
    fresh(modalUpdateForm) {
      this.modalUpdate.show = false;
      this.modalUpdate.edit = true;
      this.listByPage();
      // this.$refs[modalUpdateForm].resetFields();
      this.restForm();
    },

    enableDept(row) {
      row.orgStatus = '1'
      row.operation = 1
      this.updateById(row)
    },
    disableDept(row) {
      row.orgStatus = '0'
      row.operation = 1
      this.updateById(row)
    },

    listByPage() {
      this.loading = true;
      listOrg(this.listQuery).then((data) => {
        this.queryData = data.data;
        this.total = data.total;
        console.log('响应数据:%o', data);
        this.loading = false;
      });
    },

    getDeptUser(deptId) {
      this.$router.push({
        path: '/permision/deptUserIndex',
        query: { deptId: deptId},
      });
    },

    addDeptWindow() {
      this.modalUpdate.show = true;
      this.modalUpdate.edit = false;
      this.restForm();
    },

    // 打开编辑窗口
    updateDeptWindow(row) {
      this.modalUpdate.show = true;
      this.modalUpdate.edit = false;
      this.getUpdateFormParams(row);
    },

    // 跳转到机构流程管理页面
    goOrgProcess(row) {
      this.$router.push({
        path: '/permision/orgProcess',
        query: { orgId: row.id},
      });
    },

    addOrUpdate(modalUpdateForm) {
      var id = this.modalUpdateForm.id;
      if (id != null) {
        // 修改
        this.updateById(this.modalUpdateForm);
      } else {
        this.$refs[modalUpdateForm].validate((valid) => {
          if (valid) {
            // 提交
            createOrUpdateOrg(this.modalUpdateForm).then((data) => {
              this.$notify.success('操作成功');
              this.fresh(modalUpdateForm);
            });
          } else {
            return false;
          }
        });
      }
    },

    restForm() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 10,
        orgName: undefined,
      };

      this.modalUpdateForm = {
        orgName: undefined,
        id: undefined,
      };
      // this.listByPage();
    },

    restQueryForm() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 10,
        orgName: undefined,
      };
      this.listByPage();
    },
    getUpdateFormParams(row) {
      this.modalUpdateForm = {
        orgName: row.orgName,
        id: row.id,
      };
    },
    cellDblclick({row,rowIndex,column,columnIndex,cell},event) {
      let _this = this;
      this.$copyText(event.target.innerText).then(function() {
        _this.$message.info('复制成功');
      });
    }
  },
};
</script>

<style lang="scss">
  .addModal{
    .el-form-item__error {
      padding-top: 0;
      line-height: 0.7;
    }
  }

</style>
<style lang="css" scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
}

.pager{
  position: fixed;
  bottom: 0;
  right: 28px;
  background-color: #FAFAFB;
  z-index: 2;
  height: 40px;
  width: calc(100% - 260px);
  }

.right >>> .el-button--primary,.el-button--default {
  padding: 10px 20px;
}

.vxe-table--render-default >>> .el-button--text{
  color: #2b7bfb;
}
.app-container >>> .vxe-table--render-default,.vxe-pager {
  font-family: PingFangSC, Arial, Helvetica, sans-serif;
}

.left {
  flex: 14;
  display: flex;
  flex-flow: row wrap;
  margin-right: 20px;
}
.left >>> .el-form {
  width: 100%;
}
.left >>> .el-form .el-form-item {
  display: flex;
  flex-flow: row nowrap;
  width: 30%;
  margin-right: 3%;
  float: left;
  margin-bottom: 14px;
}

.left >>> .el-form .el-form-item .el-form-item__label {
  font-weight: 500;
  color: #606266;
  text-align: left;
  /* flex: 1; */
}

.left >>> .el-form .el-form-item .el-form-item__content {
  flex: 1;
  /*超出部分隐藏*/
  text-overflow: ellipsis;
  /* 超出部分显示省略号 */
  white-space: nowrap;
  /*规定段落中的文本不进行换行 */
  width: 100%;
}
.date_item {
  width: 100%;
}
.right {
  flex: 2;
  display: flex;
  flex-flow: row nowrap;
  align-items: flex-end;
  margin-right: 30px;
}
</style>
